//
//  DetailWebViewController.swift
//  SunxianhuaTestApp
//
//  Created by abc on 2021/10/12.
//

import Foundation
import WebKit
class DetailWebViewController: BaseViewController {
    var url : String?
    private var wkView : WKWebView!
    lazy var progressView = UIProgressView()
    
    deinit {
        wkView.removeObserver(self, forKeyPath: "estimatedProgress", context: nil)
    }
    
    init(url :String?) {
        super.init(nibName: nil, bundle: nil)
        self.url = url
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        guard let url = url else { return }
        guard let u = URL(string: url) else { return }
        var request = URLRequest(url: u)
        request.allowsCellularAccess = true
        request.cachePolicy = .reloadIgnoringLocalCacheData
        request.timeoutInterval = 20
        wkView.load(request)
    }
    
    override func setupSubViews() {
        super.setupSubViews()
        
        let config = WKWebViewConfiguration()
        config.allowsInlineMediaPlayback = true
        wkView = WKWebView(frame: .zero, configuration: config)
        wkView.navigationDelegate = self
        view.addSubview(wkView)
        wkView.snp.makeConstraints { (make) in
            make.edges.equalTo(0.0)
        }
        
        if #available(iOS 11, *) {
            wkView.scrollView.contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
        progressView.progressTintColor = UIColor.blue
        progressView.trackTintColor = .lightGray
        view.addSubview(progressView)
        progressView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(300.0)
            make.height.equalTo(5)
        }
        
        wkView.addObserver(self, forKeyPath: "estimatedProgress", options: [.new], context: nil)
    }

}


extension DetailWebViewController : WKNavigationDelegate {
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        HUD.showTip("加载完成")
        title = webView.title
        UIView.animate(withDuration: 0.5) { [weak self] in
            self?.progressView.isHidden = true
        }
    }
    
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        HUD.showTip("加载失败")
        UIView.animate(withDuration: 0.5) { [weak self] in
            self?.progressView.progress = 0.0
            self?.progressView.isHidden = true
        }
    }
    
    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        print("开始内容返回")
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if let keyPath = keyPath, keyPath == "estimatedProgress" {
            progressView.progress = Float(wkView.estimatedProgress)
            return
        }
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
    }
    
}
